package com.gnerv.oauth.utils;

import com.gnerv.oauth.cuntom.login.CustomUsernamePasswordAuthenticationToken;
import com.gnerv.oauth.entity.CustomUserDetails;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

public class SecurityUtils {

    public static UserDetails getLoginUser(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if(authentication instanceof CustomUsernamePasswordAuthenticationToken){
            CustomUsernamePasswordAuthenticationToken authenticationToken = (CustomUsernamePasswordAuthenticationToken) authentication;
            CustomUserDetails userDetails = (CustomUserDetails) authenticationToken.getPrincipal();
            userDetails.setPassword("");
            return userDetails;
        }
        return null;
    }

}
